package com.qf2.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    //在try中，在处理器方法前面执行的
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setContentType("text/html;charset=utf-8");

        System.out.println("preHandle....");

        //判断用户是否登录 ，登录了返回true，否则返回false
    /*    Object userInfo = request.getSession().getAttribute("userInfo");
        if (userInfo == null){
            //没有登录
            response.getWriter().write("请先登录...");
            return false;
        }*/

        return true;
    }

    //当处理器方法执行正常，不出现异常的时候，才会执行 在try中，在处理器方法后面执行的
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle.....");
    }

    //不管处理器是否正常执行，都会执行到 在finally中的，只要preHandle返回true了，就会执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion......");
    }
}
